﻿using HZH_Controls.Controls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变.user_Form
{
    public partial class user_returnFrm : Form
    {
        public user_returnFrm()
        {
            InitializeComponent();
        }
        public string opname { get; set; }
        public void loadMsg()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "RentID", HeadText = "订单编号", Width = 20, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CNumber", HeadText = "车牌号", Width = 20, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "carname", HeadText = "车名", Width = 50, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "opername", HeadText = "租车人", Width = 20, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Paytime", HeadText = "付款时间", Width = 20, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "begindate", HeadText = "起始时间", Width = 40, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "returntime", HeadText = "归还时间", Width = 40, WidthType = SizeType.Percent });
            this.ucDataGridView1.Columns = lstCulumns;
            this.ucDataGridView1.IsShowCheckBox = true;
            List<object> lstSource = new List<object>();
            string sql = "select * from carRents where opername=@opername and isPay='True' and rentflag =0";
            SqlParameter p = new SqlParameter("@opername", this.opname);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery(sql, p);
            this.ucDataGridView1.DataSource = dt;
            this.ucDataGridView1.First();
        }
        private void user_returnFrm_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            loadMsg();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {

            if (ucDataGridView1.SelectRows != null && ucDataGridView1.SelectRows.Count > 0)
            {
                var uids = new List<int>();
                var carids = new List<string>();
                var carname = new List<string>();
                foreach (var item in ucDataGridView1.SelectRows)
                {
                    var tempid = Convert.ToInt32((item.DataSource as DataRow)["RentID"]);
                    var numberId = (item.DataSource as DataRow)["CNumber"].ToString();
                    var cname = (item.DataSource as DataRow)["carname"].ToString();

                    uids.Add(tempid);
                    carids.Add(numberId);
                    carname.Add(cname);

                }
                DialogResult dir = MessageBox.Show("您确定要归还车辆吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dir == DialogResult.OK)
                {
                    /*RentID*/
                    var uidsstr = new StringBuilder();
                    uidsstr.Append("(");
                    foreach (var item in uids)
                    {
                        uidsstr.Append($"{item},");
                    }
                    uidsstr.Remove(uidsstr.Length - 1, 1);
                    uidsstr.Append(")");

                    /*CNumber*/
                    var uidsstr2 = new StringBuilder();
                    uidsstr2.Append("(");
                    foreach (var item in carids)
                    {
                        uidsstr2.Append($"'{item}',");
                    }
                    uidsstr2.Remove(uidsstr2.Length - 1, 1);
                    uidsstr2.Append(")");

                    /*carname*/
                    var uidsstr3 = new StringBuilder();
                    uidsstr3.Append("(");
                    foreach (var item in carname)
                    {
                        uidsstr3.Append($"'{item}',");
                    }
                    uidsstr3.Remove(uidsstr3.Length - 1, 1);
                    uidsstr3.Append(")");
                    string sql3 = $"update  carRents set rentflag = 1 where RentID in {uidsstr}";
                    int result = DBHelper.ExecuteNonQuery(sql3);
                    if (result > 0)
                    {
                        this.ShowSuccessTip("还车成功！欢迎下次使用！");
                        //修改汽车状态为空闲
                        string sql2 = $"update CarInfo set CStatus = 'True' where CNumber in {uidsstr2}";
                        DBHelper.ExecuteNonQuery(sql2);

                        string username = (ucDataGridView1.SelectRow.DataSource as DataRow)["opername"].ToString();
                        //将归还车辆信息插入检查表
                        //
                        var sql4 = new StringBuilder();
                        sql4.Append($"insert into backTable (carname,carid,username,returntime,isChecked) values");
                        for (int i = 0; i < carids.Count; i++)
                        {
                            sql4.Append($"('{carname[i]}','{carids[i]}','{username}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}','False'),");                           
                        }
                        sql4.Remove(sql4.Length - 1, 1);
                        sql4.Append(";");
                        DBHelper.ExecuteNonQuery(sql4.ToString());
                        loadMsg();
                    }
                    else
                    {
                        this.ShowErrorTip("还车失败！请联系管理员");
                    }

                }
                else
                {
                    return;
                }
            }
            else
            {
                this.ShowErrorTip("未选择任何车辆，请选择要归还的车辆再进行归还!");
            }

        }
    }
}
////rentflag变为1, 车的状态为true
//DialogResult dir = MessageBox.Show("您确定要归还车辆吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
//if (dir == DialogResult.OK)
//{
//    string id = (ucDataGridView1.SelectRow.DataSource as DataRow)["RentID"].ToString();

//    string sql = "update  carRents set rentflag = 1 where RentID=@RentID";
//    SqlParameter[] ps =
//    {
//                    new SqlParameter("@RentID",id),

//                };
//    int result = DBHelper.ExecuteNonQuery(sql, ps);
//    if (result > 0)
//    {
//        this.ShowSuccessTip("还车成功！欢迎下次使用！");
//        string carname = (ucDataGridView1.SelectRow.DataSource as DataRow)["carname"].ToString();
//        string sql2 = "update CarInfo set CStatus = 'True' where CName = @cname";
//        SqlParameter s = new SqlParameter("@cname", carname);
//        DBHelper.ExecuteNonQuery(sql2, s);
//        //插入检查表
//        string sql3 = "insert into backTable (carname,carid,username,returntime,isChecked) values (@carname,@carid,@username,@returntime,@isChecked)";
//        string carid = (ucDataGridView1.SelectRow.DataSource as DataRow)["CNumber"].ToString();
//        string username = (ucDataGridView1.SelectRow.DataSource as DataRow)["opername"].ToString();
//        SqlParameter[] ps3 =
//        {
//                        new SqlParameter("@carname",carname),
//                        new SqlParameter("@carid",carid),
//                        new SqlParameter("@username", username),
//                        new SqlParameter("@returntime",DateTime.Now),
//                        new SqlParameter("@isChecked","False"),
//                    };
//        DBHelper.ExecuteNonQuery(sql3, ps3);
//        loadMsg();

//    }
//    else
//    {
//        this.ShowErrorTip("还车失败！请联系管理员");
//    }
//}
//else
//{
//    return;
//}
